package scsys.webServlet.main;

import com.alibaba.fastjson.JSON;
import scsys.pojo.Course;
import scsys.pojo.Elective;
import scsys.pojo.User;
import scsys.servlet.*;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/remove_user_Servlet")
public class remove_user_Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = new UserServiceImpl();
        EletiveService eletiveService = new EletiveServiceImpl();
        CourseService courseService = new CourseServiceImpl();
        //1. 接收数据
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        //转为User对象
        User user = JSON.parseObject(params, User.class);
        Course course = new Course();
        Elective elective = new Elective();
        user = userService.selectOne(user);

        try {
            if("0".equals(user.getRole())) {
                // 学生
                elective.setUserName(user.getUserName());
                eletiveService.deleteElective(elective);
                userService.deleteUser(user);
            }else {
                // 教师
                course.setTeacher(user.getRealName());
                courseService.deleteCourse(course);
                userService.deleteUser(user);
            }
            response.getWriter().write("yes");
        } catch (Exception e) {
            response.getWriter().write("no");
            e.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
